
from zoom import zoomer

import dzentity, dzpage, dzutil
import models.product

#------------------------------------------------------------------------
class ProductListView(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select * from dz_products order by title')

    def get_LIST(self):
        labels = ['Title','Status','Version','Visible','System']
        items = [(i.productid,i.title,i.status,i.version,i.visible and 'Y' or '',i.system and 'Y' or '') for i in self.data]
        return dzutil.browse(items,labels=labels,con='product',on_click='show')

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/products.txt',self))
        page.actions = [dzutil.link_to('Add Application',action='add',con='product')]
        return page.render()

class ProductShowView(dzentity.Entity):
    def __init__(self,id):
        data = zoomer.db('select * from dz_products where productid=%s',id)
        dzentity.Entity.__init__(self,data[0])

    def render(self):
        page = dzpage.Page(dzutil.render('views/product_show.txt',self))
        page.actions = [
            dzutil.link_to('Edit',action='edit',key=self.PRODUCTID),
            dzutil.link_to('Delete',action='confirm_delete',key=self.PRODUCTID),
        ]
        return page.render()

class ProductDeleteView(dzentity.Entity):
    def __init__(self,id):
        user = models.product.Products().seek(id)
#        data = zoomer.db('select * from dz_products where productid=%s',id)
        dzentity.Entity.__init__(self,user)

    def render(self):
        page = dzpage.Page(dzutil.render('views/product_delete.txt',form=1,con='product',action='delete',data=self))
        return page.render()

class ProductEditView(ProductShowView):
    def render(self):
		page = dzpage.Page(dzutil.render('views/product_edit.txt',form=1,con='product',action='update',data=self))
		return page.render()

class ProductAddView:
    def render(self):
        from models.product import Product
        page = dzpage.Page()
        page.content = dzutil.render('views/product_edit.txt',form=1,con='product',action='insert',data=Product())
        return page.render()

#    def render(self):
#		return dzpage.Page(dzutil.render('views/product_show.txt',self)).render()


